package com.hcrazy.manchester.app.controllers;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.hcrazy.manchester.app.form.SimpleSearchForm;
import com.hcrazy.manchester.app.pojo.ErrorMsg;
import com.hcrazy.manchester.app.pojo.PageModel;
import com.hcrazy.manchester.app.pojo.User;
import com.hcrazy.manchester.app.services.UserService;
import com.hcrazy.manchester.app.utils.Log;

@Controller
public class UserController {
	
	@Autowired
	private UserService userService;
	
	@RequestMapping(value="/register")
	public void register(){
		
	}
	
	@RequestMapping(value="/login")
	public void login(){
		
	}
	
	@RequestMapping(value="/accounts/email_register")
	public void email_register(){
		
	}
	
	@RequestMapping(value="/accounts/phone_register")
	public void phone_register(){
		
	}
	
	@RequestMapping(value="/accounts/common_register")
	public void common_register(){
		
	}
	
	@RequestMapping(value="/addUserInfo")
	public void addUserInfo(){
		
	}
	
	@RequestMapping(value="/accounts/register")
	public String confirmRegister(@RequestParam String confirmation,Model model){
		try {
			User user = userService.confirmUser(confirmation);
			model.addAttribute("user",user);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			
		}
		return "/accounts/confirm";
	}
	
	@RequestMapping(value="/accounts/checkEmail")
	public void checkEmail(@RequestParam String email,Model model){
		model.addAttribute("email", email);
	}
	
	@RequestMapping(value="/addFriends")
	public void addFriends(){
		
	}
	
	@RequestMapping(value="/addFriend")
	public void addFriend(){
		
	}
	
	@RequestMapping(value="/setFriend")
	public void setFriend(){
		
	}
	
	@RequestMapping(value="/addGroup")
	public void addGroup(){
		
	}
	
	@RequestMapping(value="/addGroupUser")
	public void addGroupUser(){
		
	}
	
	@RequestMapping(value="/allowGroupUser")
	public void allowGroupUser(){
		
	}
	
	/**
	 * 后台管理
	 */
	
	@RequestMapping("/admin/member/user")
	public void user(Model model){
		PageModel pageModel = getAllUsers(1, "");
		model.addAttribute("pageModel", pageModel);
		
		//初始化查询参数
		model.addAttribute("searchForm", new SimpleSearchForm());
	}
	
	@RequestMapping("/admin/member/addUser")
	public void addUser(Model model){
		model.addAttribute("user", new User());
	}
	
	/**
	 * 编辑信息内容
	 * @param infoId 信息标识
	 * @param model 数据返回对象
	 * @return
	 */
	@RequestMapping(value="/admin/member/editUser",method=RequestMethod.GET)
	public String editUser(@RequestParam long userId,Model model){
		try {
			//初始化对象内容
			addUser(model);
			//根据id获取信息内容
			User user = userService.getUserById(userId);
			//更新返回对象
			model.addAttribute("user",user);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
		}
		return "/admin/member/addUser";
	}
	
	/**
	 * 提交信息内容
	 * @param simple
	 * @return
	 */
	@RequestMapping(value="/admin/member/user",method=RequestMethod.POST)
    public String addOrUpdateSort(@ModelAttribute("form") User user,Model model){
		try {
			Object obj = userService.addOrUpdateUser(user);
			if(obj instanceof ErrorMsg){
				model.addAttribute("message", ((ErrorMsg)obj).getMsg());
				return "/admin/failed";
			}
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			model.addAttribute("message", e.getMessage());
			return "/admin/failed";
		}
		return "redirect:/admin/member/user";
    }
	
	/**
	 * 查询信息列表
	 * @param form 查询参数form
	 * @param pageNum 当前分页
	 * @param model 请求返回对象
	 * @return
	 */
	@RequestMapping(value="/admin/member/search",method=RequestMethod.POST)
	public String search(@ModelAttribute("form") SimpleSearchForm form,Model model){
		//是否执行删除操作
		if(form.getInfoId() > 0){
			try {
				userService.deleteUser(form.getInfoId());
			} catch (Exception e) {
				Log.error(e.getMessage(), e);
			}
		}
		//重置infoId
		form.setInfoId(0);
		
		//根据查询参数获取分页对象
		PageModel pageModel = getAllUsers(form.getPageNum(),form.getSearch());
		model.addAttribute("pageModel", pageModel);
		
		//回传查询参数
		model.addAttribute("searchForm",form);
		
		return "/admin/member/user";
	}
	
	private PageModel getAllUsers(int pageNum,String search){
		PageModel model = new PageModel();
		model.setPageNum(pageNum);
		try {
			return userService.gePagedUsers(model, search);
		} catch (Exception e) {
			Log.error(e.getMessage(), e);
			return null;
		}
	}
	
	@RequestMapping("/addRent")
	public void addRent(){
		
	}
	
	@RequestMapping("/addSecondary")
	public void addSecondary(){
		
	}
	
	@RequestMapping("/uploadImage")
	public void uploadImage(){
		
	}
	
	@RequestMapping("/addCollege")
	public void addCollege(){
		
	}
	
	@RequestMapping("/attachment")
	public void attachment(){
		
	}
}
